package org.ttitfly.ttcore.web.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.ttitfly.ttcore.service.menu.MenuManager;
import org.ttitfly.ttcore.vo.menu.MenuItemVO;

import com.best.oasis.tablemat.menu.MenuComponent;
import com.best.oasis.tablemat.menu.MenuRepository;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
 * 登录成功后直接执行菜单分配
 * @author ttitfly
 *
 */
public class MainAction extends ActionSupport implements ServletRequestAware, SessionAware {

    private static final long serialVersionUID = -6437793219156006106L;

    private String redirect;

    private Map<String, Object> session = null;

    private HttpServletRequest httpServletRequest = null;

//    private MenuItemService menuItemService;
    @Autowired
    private MenuManager menuManager;

    private String appName = "all";

    public String getRedirect() {
        return redirect;
    }

    public void setRedirect(String redirect) {
        this.redirect = redirect;
    }

    @Override
    public String execute() throws Exception {

        ActionContext cxt = ActionContext.getContext();
        Object obj = cxt.getApplication().get("appName");
        List<MenuItemVO> menuItemList = new ArrayList<MenuItemVO>();
        menuItemList = menuManager.getAll();
        
        String username = "ttitfly";
        initMenu(menuItemList, username);

        // find childMenu
        redirect = findChildMenu();
        if (redirect != null) {
            return "redirect";
        } else {
            return SUCCESS;
        }
    }

    private String findChildMenu() throws Exception {
        String childMenuLocation = null;
        String topMenu = httpServletRequest.getParameter(MenuRepository.TOP_PARAMETER);
        if (topMenu == null || topMenu.length() == 0) {
            return childMenuLocation;
        }

        MenuRepository menuRepository = (MenuRepository) session.get(MenuRepository.MENU_REPOSITORY_KEY);
        if (menuRepository != null) {
            MenuComponent menu = menuRepository.getMenu(topMenu);

            String location = menu.getLocation();
            int seat = location.indexOf(MenuRepository.CHILD_MENU_NAME);
            String childMenu = "";
            if (seat > -1) {
                childMenu = location.substring(seat);

                if (childMenu.indexOf("&") > 0) {
                    childMenu = childMenu.substring(0, childMenu.indexOf("&"));
                }
                if (childMenu.indexOf("=") > 0) {
                    childMenu = childMenu.substring(childMenu.indexOf("=") + 1);
                }
            }
            if (childMenu.length() > 0) {
                menu = menuRepository.getMenu(childMenu);
                childMenuLocation = menu.getLocation();
            }
        }
        return childMenuLocation;
    }

    private void initMenu(List<MenuItemVO> menuItemList, String username) {
        MenuManager menuMgr = new MenuManager();
        MenuRepository menuRepository = menuMgr.getRepository(httpServletRequest, menuItemList, username);

        String sessionTop = null;
        String sessionSub = null;

        MenuComponent menu = menuRepository.getTopMenusAsArray()[0];
        sessionTop = menu.getName();
        sessionSub = menu.getName();

        if (session.get(MenuRepository.MENU_REPOSITORY_KEY) != null) {
            session.remove(MenuRepository.MENU_REPOSITORY_KEY);
        }
        if (session.get(MenuRepository.MENU_SESSION_TOP) != null) {
            session.remove(MenuRepository.MENU_SESSION_TOP);
        }
        if (session.get(MenuRepository.MENU_SESSION_SUB) != null) {
            session.remove(MenuRepository.MENU_SESSION_SUB);
        }

        session.put(MenuRepository.MENU_REPOSITORY_KEY, menuRepository);
        session.put(MenuRepository.MENU_SESSION_TOP, sessionTop);
        session.put(MenuRepository.MENU_SESSION_SUB, sessionSub);
    }

    @SuppressWarnings("unchecked")
    public void setSession(Map arg0) {
        this.session = arg0;
    }

    public void setServletRequest(HttpServletRequest arg0) {
        this.httpServletRequest = arg0;
    }

//    public void setMenuItemService(MenuItemService menuItemService) {
//        this.menuItemService = menuItemService;
//    }
}